package servlet;

import Dao.JdbcTools;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
@WebServlet("/vueupdate")
public class Vueupdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");//响应用户请求，告知用户服务器的编码
        System.out.println(123456);
        String data1 = req.getParameter("data");
        System.out.println("data1 = " + data1);
        //json格式转换json对象
        JSONObject jsonObject = JSONObject.parseObject(data1.toString());
        System.out.println("jsonObject = " + jsonObject);
        //获取对象中各个属性的值
        String id = jsonObject.getString("id");
        String name = jsonObject.getString("name");
        String age = jsonObject.getString("age");
        String height = jsonObject.getString("height");
        String school = jsonObject.getString("school");
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        System.out.println("height = " + height);
        System.out.println("school = " + school);

        try {
            int update = JdbcTools.del("update vue set name=?,age=?,height=?,school=? where id=?",name,age,height,school,id);
            System.out.println(" update= " + update);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //数据输出

        PrintWriter writer = resp.getWriter();
        writer.write("success");
    }

}
